package com.geez.bbl.pdf;

import com.geez.bbl.util.ProjectResources;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.*;

/**
 * @author biniam.gebremichael
 * @since 9/11/12
 */
public class HaHuCardPdf {
    public static String ti = "ሀ,he;ሁ,hu;ሂ,hi;ሃ,ha;ሄ,hE;ህ,h;ሆ,ho;ለ,le;ሉ,lu;ሊ,li;ላ,la;ሌ,lE;ል,l;ሎ,lo;ሐ,He;ሑ,Hu;ሒ,Hi;ሓ,Ha;ሔ,HE;ሕ,H;ሖ,Ho;መ,me;ሙ,mu;ሚ,mi;ማ,ma;ሜ,mE;ም,m;ሞ,mo;ሰ,se;ሱ,su;ሲ,si;ሳ,sa;ሴ,sE;ስ,s;ሶ,so;ረ,re;ሩ,ru;ሪ,ri;ራ,ra;ሬ,rE;ር,r;ሮ,ro;ሸ,she;ሹ,shu;ሺ,shi;ሻ,sha;ሼ,shE;ሽ,sh;ሾ,sho;ቸ,che;ቹ,chu;ቺ,chi;ቻ,cha;ቼ,chE;ች,ch;ቾ,cho;ለ,le;ሉ,lu;ሊ,li;ላ,la;ሌ,lE;ል,l;ሎ,lo;ቀ,qe;ቁ,qu;ቂ,qi;ቃ,qa;ቄ,qE;ቅ,q;ቆ,qo;ቐ,Qe;ቑ,Qu;ቒ,Qi;ቓ,Qa;ቔ,QE;ቕ,Q;ቖ,Qo;በ,be;ቡ,bu;ቢ,bi;ባ,ba;ቤ,bE;ብ,b;ቦ,bo;ተ,te;ቱ,tu;ቲ,ti;ታ,ta;ቴ,tE;ት,t;ቶ,to;ነ,ne;ኑ,nu;ኒ,ni;ና,na;ኔ,nE;ን,n;ኖ,no;ኘ,Ne;ኙ,Nu;ኚ,Ni;ኛ,Na;ኜ,NE;ኝ,N;ኞ,No;አ,e;ኡ,u;ኢ,i;ኣ,a;እ,I;ኤ,E;ኦ,o;ከ,ke;ኩ,ku;ኪ,ki;ካ,ka;ኬ,kE;ክ,k;ኮ,ko;ኸ,Ke;ኹ,Ku;ኺ,Ki;ኻ,Ka;ኼ,KE;ኽ,K;ኾ,Ko;ወ,we;ዉ,wu;ዊ,wi;ዋ,wa;ዌ,wE;ው,w;ዎ,wo;ዐ,'e;ዑ,'u;ዒ,'i;ዓ,'a;ዔ,'E;ዕ,'I;ዖ,'o;ዘ,ze;ዙ,zu;ዚ,zi;ዛ,za;ዜ,zE;ዝ,z;ዞ,zo;ዠ,Ze;ዡ,Zu;ዢ,Zi;ዣ,Za;ዤ,ZE;ዥ,Z;ዦ,Zo;የ,ye;ዩ,yu;ዪ,yi;ያ,ya;ዬ,yE;ይ,y;ዮ,yo;ደ,de;ዱ,du;ዲ,di;ዳ,da;ዴ,dE;ድ,d;ዶ,do;ጀ,je;ጁ,ju;ጂ,ji;ጃ,ja;ጄ,jE;ጅ,j;ጆ,jo;ገ,ge;ጉ,gu;ጊ,gi;ጋ,ga;ጌ,gE;ግ,g;ጎ,go;ጠ,Te;ጡ,Tu;ጢ,Ti;ጣ,Ta;ጤ,TE;ጥ,T;ጦ,To;ጨ,Ce;ጩ,Cu;ጪ,Ci;ጫ,Ca;ጬ,CE;ጭ,C;ጮ,Co;ጸ,Se;ጹ,Su;ጺ,Si;ጻ,Sa;ጼ,SE;ጽ,S;ጾ,So;ፈ,fe;ፉ,fu;ፊ,fi;ፋ,fa;ፌ,fE;ፍ,f;ፎ,fo;ቨ,ve;ቩ,vu;ቪ,vi;ቫ,va;ቬ,vE;ቭ,v;ቭ,vo;ጰ,Pe;ጱ,Pu;ጲ,Pi;ጳ,Pa;ጴ,PE;ጵ,P;ጶ,Po;ፐ,pe;ፑ,pu;ፒ,pi;ፓ,pa;ፔ,pE;ፕ,p;ፖ,po";
    public static String [] images = new String[]{"butterflies.gif","caw.jpg", "dog.gif","elephant.jpg","lion.gif","turtle.gif","rabbit.png"};
    public int counter = 0;
    Image [] bgImage = new Image[7];
    public static String nyalaFile = ProjectResources.getResourceFile("itext", "fonts", "nyala.ttf").getAbsolutePath();
    public static Font nyala = FontFactory.getFont(nyalaFile, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 30,Font.NORMAL,BaseColor.ORANGE);
    public static final File HahuFile = ProjectResources.getResourceFile("itext", "hahu.pdf");
    Document document;
    PdfWriter writer;

    public HaHuCardPdf() throws IOException, DocumentException {
        document = new Document(PageSize.A4);
        writer = PdfWriter.getInstance(document, new FileOutputStream(HahuFile));
                             counter = 0;
        for (int i=0;i<7;i++) {
            bgImage[i] = Image.getInstance(ProjectResources.getResourceFile("itext", "image",images[i]).getAbsolutePath());
        }
        document.open();
    }

    public void close() {
        document.close();
    }

    public void createPdf() throws DocumentException {
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        newPage( );
        close();
    }

    private void newPage( ) {
        float tableWidth = 120f;
        float tableHeight = 200f;
        float marigin = 20f;
        getRow(tableWidth, 0, marigin);
        getRow(tableWidth, tableHeight, marigin);
        getRow(tableWidth, tableHeight * 2, marigin);
        getRow(tableWidth, tableHeight * 3, marigin);
        document.newPage();
    }


    public void getRow(float tableWidth, float tableHeight, float marigin) {
          paintTable(tableWidth, marigin, 800 - tableHeight);
        paintTable(tableWidth, tableWidth + 2* marigin, 800 - tableHeight);
        paintTable(tableWidth, 2 * tableWidth + 3 *marigin, 800 - tableHeight);
        paintTable(tableWidth, 3 * tableWidth + 4* marigin, 800 - tableHeight);

    }

    private void paintTable(final float w, final float x, final float y) {
        PdfPTable table = createFirstTable(w);
       // table.setTableEvent(new ImagePainter(x,y,w));
        table.writeSelectedRows(0, -1, x, y, writer.getDirectContent());
    }


    public static void main(String[] args) throws Exception {
        HaHuCardPdf haHuCardPdf = new HaHuCardPdf();
        haHuCardPdf.createPdf();
    }



    public PdfPTable createFirstTable(float tableWidth) {
        PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(tableWidth);
        if(counter>=ti.split(";").length) {
            return table;
        }
        String string = ti.split(";")[counter];
        PdfPCell  ha = new PdfPCell(new Phrase(string.split(",")[0],nyala));
                ha.setBorder(0);
        PdfPCell  en = new PdfPCell(new Phrase(string.split(",")[1],new Font(Font.FontFamily.HELVETICA,20,Font.ITALIC,BaseColor.GRAY)));
        en.setBorder(0);

        ha.setVerticalAlignment(Element.ALIGN_TOP);
        en.setVerticalAlignment(Element.ALIGN_MIDDLE);
        ha.setHorizontalAlignment(Element.ALIGN_LEFT);
        en.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(ha);
        table.addCell(en);
        PdfPCell cell = new PdfPCell(new Phrase(""));
        cell.setColspan(2);
        cell.setBorder(0);
        cell.setCellEvent(new ImagePainter(tableWidth, 20f, 20f));
        cell.setFixedHeight(110f);
        table.addCell(cell);
        ha.setHorizontalAlignment(Element.ALIGN_RIGHT);
        en.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(en);
        table.addCell(ha);
        counter++;

        return table;
    }

    class ImagePainter implements  PdfPTableEvent , PdfPCellEvent {
        float x,y,w;

        ImagePainter(float x, float y, float w) {
            this.x = x;
            this.y = y;
            this.w = w;
        }

        @Override
                  public void tableLayout(PdfPTable pdfPTable, float[][] floats1, float[] floats2, int i, int i1, PdfContentByte[] pdfContentBytes) {
            getPdfContentBytes(pdfContentBytes);
                  }

        @Override
        public void cellLayout(PdfPCell arg0, Rectangle arg1, PdfContentByte[] arg2) {
            try {
                        PdfContentByte pdfContentByte = arg2[PdfPTable.BACKGROUNDCANVAS];
                        pdfContentByte.addImage(bgImage[counter%7], arg1.getWidth(), 0, 0, arg1
                                .getHeight(), arg1.getLeft(), arg1.getBottom());

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

        }

        private void getPdfContentBytes(PdfContentByte[] pdfContentBytes) {
            PdfContentByte pdfContentByte = pdfContentBytes[PdfPTable.BACKGROUNDCANVAS];
            try {

                pdfContentByte.addImage(bgImage[counter%7], w, 0, 0, w, x, y,false);
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
    }


}

